/*
 * Copyright (C) 2009 Tony Gentilcore
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.scoreninja.adapter;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;

/**
 * Creates an alert message that gives the user the option to install the
 * missing ScoreNinja package that is needed through the Android Market.
 * 
 * @author tonygentilcore@gmail.com (Tony Gentilcore)
 */
final class ScoreNinjaInstallAlert extends Builder {
  // These strings must be in the Java file itself in order for this to be
  // packed into a .jar file.
  private final static String MARKET_URI = "market://search?q=pname:" + ScoreNinjaAdapter.PACKAGE;
  private final static String NO_SCORE_NINJA = "This game can use ScoreNinja to track global high scores. Please install ScoreNinja from the market.";
  private final static String NOW = "Install Now";
  private final static String LATER = "Maybe Later";
  private final static String NEVER = "Never Install";

  private final Activity parent;

  ScoreNinjaInstallAlert(Context context) {
    super(context);
    parent = (Activity) context;
    setMessage(NO_SCORE_NINJA);

    OnClickListener installListener = new OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        Uri marketUri = Uri.parse(MARKET_URI);
        Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
        parent.startActivity(marketIntent);
      }
    };

    OnClickListener laterListener = new OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
      }
    };
    
    OnClickListener neverListener = new OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        SharedPreferences settings = parent.getSharedPreferences(ScoreNinjaAdapter.PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(ScoreNinjaAdapter.NEVER_ASK_PREF, true);
        editor.commit();
      }
    };

    setPositiveButton(NOW, installListener);
    setNeutralButton(LATER, laterListener);
    setNegativeButton(NEVER, neverListener);
  }
}